using System;

namespace SharpWind.Network
{
	public class VoiceManager : IDisposable
	{
		private Voice voice = null;

		private VoiceCoder voiceCoder = null;

		public VoiceManager() : this( 44100, 4, 1 )
		{
		}

		public VoiceManager( int sampleSize, int sampleRate, int channels )
		{
			this.voice = new Voice( sampleRate, sampleSize, channels );
			this.voiceCoder = new VoiceCoder( sampleRate );

			this.voice.VoiceCaptured += new VoiceEventHandler(Voice_VoiceCaptured);
			this.voiceCoder.VoiceDecoded += new VoiceEventHandler( VoiceCoder_VoiceDecoded );
			this.voiceCoder.VoiceEncoded += new VoiceEventHandler( VoiceCoder_VoiceEncoded );
			Engine.Instance.Network.VoiceReceived += new VoiceEventHandler( Network_VoiceReceived );
		}

		private void Voice_VoiceCaptured( object sender, VoiceEventArgs e )
		{
			//this.voiceCoder.Encode( e.Buffer );
			Engine.Instance.Network.SendVoice( e.Buffer );
		}

		private void VoiceCoder_VoiceDecoded( object sender, VoiceEventArgs e )
		{
			this.voice.Playback( e.Buffer );
		}

		private void VoiceCoder_VoiceEncoded( object sender, VoiceEventArgs e )
		{
			Engine.Instance.Network.SendVoice( e.Buffer );
		}

		private void Network_VoiceReceived( object sender, VoiceEventArgs e )
		{
			//this.voiceCoder.Decode( e.Buffer );
			this.voice.Playback( e.Buffer );
		}

		#region IDisposable Members

		public void Dispose()
		{
			if( this.voiceCoder != null )
			{
				this.voiceCoder.Dispose();
				this.voiceCoder = null;
			}

			if( this.voice != null )
			{
				this.voice.Dispose();
				this.voice = null;
			}
		}

		#endregion
	}
}
